local skel = fk.CreateSkill {
  name = "rmt__jieshihuanhun",
}

Fk:loadTranslationTable{
  ["rmt__jieshihuanhun"] = "借尸还魂",
  [":rmt__jieshihuanhun"] = "出牌阶段，你可以将两张牌当一张基本牌或普通锦囊牌使用，但需满足：<br>"..
  "1.至少有一张牌与你上次的转化底牌牌名相同；<br>2.至少有一个目标与你上次以此使用牌时不同。",

  ["#rmt__jieshihuanhun0"] = "借尸还魂：将两张牌当基本牌或普通锦囊牌使用",
  ["#rmt__jieshihuanhun1"] = "借尸还魂：将两张牌当基本牌或普通锦囊牌使用（必须包含【%arg】）<br>若目标只包含上次目标则失效！",
  ["#rmt__jieshihuanhun2"] = "借尸还魂：将两张牌当基本牌或普通锦囊牌使用（必须包含【%arg】或【%arg2】）<br>若目标只包含上次目标则失效！",
  ["rmt__jieshihuanhun_tip"] = "上次目标",
}


skel:addEffect("active", {
  anim_type = "offensive",
  card_num = 2,
  interaction = function(self, player)
    local all_names = table.filter(Fk:getAllCardNames("bt"), function (name)
      return player:canUse(Fk:cloneCard(name))
    end)
    local choices = player:getViewAsCardNames(skel.name, all_names)
    if #choices > 0 then
      return UI.CardNameBox {
        choices = choices, all_choices = all_names,
      }
    end
  end,
  prompt = function (self, player)
    if player:getMark(skel.name) == 0 then
      return "#rmt__jieshihuanhun0"
    else
      local names = player:getMark(skel.name)[1]
      if #names == 1 then
        return "#rmt__jieshihuanhun1:::"..names[1]
      elseif #names == 2 then
        return "#rmt__jieshihuanhun2:::"..names[1]..":"..names[2]
      end
    end
    return " "
  end,
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if player:getMark("rmt__jieshihuanhun") ~= 0 then
      if table.contains(player:getMark("rmt__jieshihuanhun")[2], to_select) then
        return "rmt__jieshihuanhun_tip"
      end
    end
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    if #selected == 0 then return true end
    if #selected == 1 then
      if player:getMark(skel.name) ~= 0 then
        local names = player:getMark(skel.name)[1]
        return table.contains(names, Fk:getCardById(to_select).trueName) or table.contains(names, Fk:getCardById(selected[1]).trueName)
      end
      return true
    end
  end,
  target_filter = function (self, player, to_select, selected, cards)
    if self.interaction.data == nil or #cards ~= 2 then return false end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    card:addSubcards(cards)
    if card:getFixedTargets(player) then return false end
    return card.skill:targetFilter(player, to_select, selected, cards, card)
  end,
  feasible = function (self, player, selected, cards)
    if self.interaction.data == nil or #cards ~= 2 then return false end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    local mark = player:getMark(skel.name)
    if mark ~= 0 then
      local tos = card:getFixedTargets(player)
      if tos == nil then tos = selected end
      tos = table.simpleClone(tos)
      if card.skill:getMinTargetNum(player) == 2 then tos = {tos[1]} end
      if table.every(tos, function (id) return table.contains(mark[2], id) end) then
        return false
      end
    end
    return card.skill:feasible(player, selected, cards, card)
  end,
  can_use = function(self, player)
    return not player:isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(effect.cards)
    local tos = card:getFixedTargets(player) or effect.tos
    local real_tos = table.simpleClone(tos) -- 真正的目标
    if card.skill:getMinTargetNum(player) == 2 then
      real_tos = {real_tos[1]}
    end
    local names = {}
    for _, id in ipairs(card.subcards) do
      table.insertIfNeed(names, Fk:getCardById(id).trueName)
    end
    room:setPlayerMark(player, skel.name, {names, real_tos})
    room:useCard{
      from = player,
      tos = tos,
      card = card,
    }
  end,
})

return skel
